package bupt.xixi.loadbalancer;

import bupt.xixi.enumeration.RpcError;
import bupt.xixi.exception.RpcException;

import java.util.List;

public class RoundRobinBalancer implements LoaderBalancer {

    private int index = 0;

    @Override
    public String select(List<String> instances) {
        if (instances.size() == 0) {
            throw new RpcException(RpcError.SERVICE_NOT_FOUND);
        }
        if (index >= instances.size()) {
            index %= instances.size();
        }
        return instances.get(index++);
    }
}
